﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zhy.DAL.Models;
using Common.Extension;
using Microsoft.EntityFrameworkCore;
using Zhy.DAL;

namespace Zhy.BLL
{
    public class StudentBLL
    {
        ZhyDbContext context = new ZhyDbContext();

        /// <summary>
        /// 查询学生信息列表
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="classId"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public List<StudentInfo> GetStudentList(string keywords, int classId, bool isShowDel)
        {
            int isDeleted = isShowDel ? 1 : 0;
            Expression<Func<StudentInfo, bool>> criteria = p => true;
            //string strWhere = "IsDeleted=" + isDeleted;
            if (!string.IsNullOrEmpty(keywords))
            {
                criteria = criteria.And(p => EF.Functions.Like(p.StuName, "%" + keywords + "%") || EF.Functions.Like(p.Phone, "%" + keywords + "%"));
            }
            if (classId > 0)
            {
                criteria = criteria.And(p => p.ClassId == classId);
            }
            var list = context.StudentInfo.Where(criteria).ToList();
            //需要查询的列名字符串
            //string cols = CreateSql.GetColsString<StudentInfo>("CreateTime,IsDeleted");
            //SqlParameter para = new SqlParameter("@keywords", $"%{keywords}%");
            //return GetModelList(strWhere, cols, para);
            return list;
        }

        /// <summary>
        /// 逻辑删除或恢复学生信息
        /// </summary>
        /// <param name="stuId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteStudent(int stuId, int isDeleted)
        {
            ////查询
            StudentInfo company1 = await context.StudentInfo.FirstOrDefaultAsync(c => c.StuId== stuId);
            company1.IsDeleted = isDeleted;
            context.SaveChanges();   //提交保存 
            return true;
        }

        /// <summary>
        /// 移除学生信息（真删除）
        /// </summary>
        /// <param name="stuId"></param>
        /// <returns></returns>
        public async Task<bool> RemoveStudent(int stuId)
        {
            ////查询
            StudentInfo company1 = await context.StudentInfo.FirstOrDefaultAsync(c => c.StuId == stuId);
            context.Remove(company1);
            context.SaveChanges();   //提交保存 

            return true;
        }
    }
}
